package com.hfh.main.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hfh.main.common.resp.Result;
import com.hfh.main.common.utils.JWTUtils;
import com.hfh.main.user.entity.UserEntity;
import com.hfh.main.user.entity.UserMenuEntity;
import com.hfh.main.user.entity.UserRmEntity;
import com.hfh.main.user.entity.UserRoleEntity;
import com.hfh.main.user.resp.UserMenuResp;
import com.hfh.main.user.service.UserMenuService;
import com.hfh.main.user.service.UserRmService;
import com.hfh.main.user.service.UserRoleService;
import com.hfh.main.user.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import static com.hfh.main.common.resp.ResultCode.TOKEN_ERROR;


@Tag(name="用户管理")
@RestController
public class LoginUserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private UserRmService userRmService;

    @Autowired
    private UserMenuService userMenuService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @PostMapping("/loginUser")
    public Result<?> loginUser(@RequestParam String acc, @RequestParam String pwd, @RequestParam String code, HttpSession session) {
        //验证码比对
//        if (!session.getAttribute("code").equals(code)) {
//            return Result.failure(200,"验证码错误");
//        }
        //获取个人信息
        QueryWrapper<UserEntity> wrapperUser = new QueryWrapper<>();
        wrapperUser.eq("user_acc", acc);
        UserEntity user = userService.getOne(wrapperUser);
        //账号存在性
        if (user == null) return Result.failure(200,"账号不存在，登陆失败");
        //密码比对
        if (!pwd.equals(user.getPwd())) return Result.failure(TOKEN_ERROR.getCode(),"密码错误，登陆失败");
        //生成token
        HashMap<String, String> map = new HashMap<>();
        map.put("id", user.getId().toString());
        map.put("acc", user.getAcc());
        map.put("pwd", user.getPwd());
        String token = JWTUtils.generateToken(map);
        // 返回数据
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("user", user);
        //获取角色
        QueryWrapper<UserRoleEntity> wrapperRole = new QueryWrapper<>();
        wrapperRole.eq("role_id", user.getRole());
        UserRoleEntity role = userRoleService.getOne(wrapperRole);
        hashMap.put("role", role);
        //获取角色-菜单
        QueryWrapper<UserRmEntity> wrapperRM = new QueryWrapper<>();
        wrapperRM.eq("rm_role_id", role.getId());
        List<UserRmEntity> listRm = userRmService.list(wrapperRM);
        //获取菜单
        ArrayList<UserMenuEntity> listMenu = new ArrayList<>();
        for (int i = 0; i < listRm.size(); i++) {
            UserRmEntity userRm = listRm.get(i);
            Integer menuId = userRm.getMenuId();
            QueryWrapper<UserMenuEntity> wrapperMenu = new QueryWrapper<>();
            wrapperMenu.eq("menu_id", menuId);
            wrapperMenu.eq("undock", 0);
            UserMenuEntity menu = userMenuService.getOne(wrapperMenu);
            listMenu.add(menu);
        }
        //在mP新建空间保存ch
        ArrayList<UserMenuResp> parent = new ArrayList<>();
        ArrayList<UserMenuEntity> son = new ArrayList<>();
        ArrayList<UserMenuEntity> other = new ArrayList<>();
        //分离mp、和、mS mO
        for (int i = 0; i < listMenu.size(); i++) {
            UserMenuEntity menu = listMenu.get(i);
            if(menu != null){
                if (Objects.equals(menu.getType(), "#")) {
                    UserMenuResp resp = new UserMenuResp();
                    BeanUtils.copyProperties(menu, resp);
                    parent.add(resp);
                } else if (Objects.equals(menu.getType(), "0")) {
                    other.add(menu);
                } else {
                    son.add(menu);
                }
            }
        }
        //将mS放入mP
        for (int i = 0; i < parent.size(); i++) {
            UserMenuResp menuP = parent.get(i);
            ArrayList<UserMenuEntity> children = new ArrayList<>();
            for (int j = 0; j < son.size(); j++) {
                UserMenuEntity menuS = son.get(j);
                if (Objects.equals(menuS.getType(), menuP.getId().toString())) {
                    children.add(menuS);
                }
            }
//            parent.get(i).setChildren(children);
        }
        //将mO放入mP
        for (int i = 0; i < other.size(); i++) {
            UserMenuEntity menu = other.get(i);
            UserMenuResp resp = new UserMenuResp();
            BeanUtils.copyProperties(menu, resp);
            parent.add(resp);
        }
        hashMap.put("menu", parent);
        return Result.success("登陆成功", hashMap);
    }

}
